package com.enwei.activitys;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.enwei.adapters.VideoListAdapter;
import com.enwei.base.BaseActivity;
import com.enwei.utils.Constans;
import com.enwei.utils.http.HttpApi;
import com.enwei.utils.http.HttpCallBack;
import com.enwei.views.NoScrollGridView;
import com.ew.action.R;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

public class VideolineActivity extends BaseActivity {
	// ListView的Adapter
	private NoScrollGridView mGridView;
	private RadioGroup mRadioGroup;
	private ScrollView mContentLayout;
	private VideoListAdapter mAdapter;
	private Context context;
	private JSONArray playList = new JSONArray();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_videoline,true,true);
		context = this;
		mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		mGridView = (NoScrollGridView) findViewById(R.id.gridView);
		mContentLayout = (ScrollView) findViewById(R.id.contentLayout);
		initData();
	}

	
	
	public void initData(){
		requestData();
		requestPlayData("");
		mAdapter = new VideoListAdapter(playList, this);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				try {
					JSONObject jsonObj = (JSONObject) mAdapter.getItem(arg2);
					Intent intent = new Intent(getApplicationContext(), VideoDetailActivity.class);
					intent.putExtra(VideoDetailActivity.DETAIL_REQUEST_ID_KEY, jsonObj.getString("id"));
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void requestData(){
		initView();
		HttpApi http = new HttpApi(this,HttpMethod.GET);
		http.sendHttpRequest(Constans.VIDEO_TYPE_LIST, null, new HttpCallBack() {
			
			@Override
			public void onSuccess(String data) {
				try {
					JSONArray array = new JSONArray(data);
					if(array != null && array.length() > 0){
						mRadioGroup.removeAllViews();
						for (int i = 0; i < array.length(); i++) {
							RadioButton button = (RadioButton) LayoutInflater.from(context).inflate(R.layout.view_videoline_cell_btton, mRadioGroup,false);
							final JSONObject json = array.getJSONObject(i);
							button.setText(json.getString("title"));
							button.setTag(json.getString("id"));
//							button.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//								
//								@Override
//								public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
//									try {
//										initView();
//										requestPlayData(json.getString("id"));
//									} catch (Exception e) {
//										e.printStackTrace();
//									}
//								}
//							});
							button.setOnClickListener(new OnClickListener() {
								
								@Override
								public void onClick(View arg0) {
									try {
										initView();
										requestPlayData(json.getString("id"));
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							});
							mRadioGroup.addView(button);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure() {
				super.onFailure();
			}
		});
	}
	
	
	private void requestPlayData(String id){
		initView();
		String url = Constans.VIDEO_PLAY_VIDEO_LIST;
		if(!TextUtils.isEmpty(id))url = url+"?id="+id;
		HttpApi http = new HttpApi(this,HttpMethod.GET);
		http.sendHttpRequest(url, null, new HttpCallBack() {
			
			@Override
			public void onSuccess(String data) {
				
				getLoadingView().setVisibility(View.GONE);
				try {
					JSONArray array = new JSONArray(data);
					if(array != null && array.length() > 0){
						playList = array;
						mAdapter.updateView(playList);
						mContentLayout.setVisibility(View.VISIBLE);
					}else{
						getEmpty().setVisibility(View.VISIBLE);
						mContentLayout.setVisibility(View.GONE);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure() {
				getErrorLayout().setVisibility(View.VISIBLE);
				getLoadingView().setVisibility(View.GONE);
				super.onFailure();
			}
		});
	}
	
	public void initView(){
		playList = new JSONArray();
		getErrorLayout().setVisibility(View.GONE);
		getLoadingView().setVisibility(View.VISIBLE);
		getEmpty().setVisibility(View.GONE);
		mContentLayout.setVisibility(View.GONE);
	}
	
	@Override
	protected void onRetry() {
		requestData();
		requestPlayData("");
		super.onRetry();
	}
}